﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CarFrameWork.Common;

namespace CarFrameWork.Model
{
	/// <summary>
	/// 类UserRight。
	/// </summary>
	[Serializable]
	public partial class UserRight
	{
		public UserRight()
		{}
		#region Model
		private int _userrightid;
		private int? _userid;
		private int? _moduleid;
		/// <summary>
		/// 
		/// </summary>
		public int UserRightID
		{
			set{ _userrightid=value;}
			get{return _userrightid;}
		}
		/// <summary>
		/// 
		/// </summary>
		public int? UserID
		{
			set{ _userid=value;}
			get{return _userid;}
		}
		/// <summary>
		/// 
		/// </summary>
		public int? ModuleID
		{
			set{ _moduleid=value;}
			get{return _moduleid;}
		}
		#endregion Model


		#region  Method

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public UserRight(int UserRightID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select UserRightID,UserID,ModuleID ");
			strSql.Append(" FROM [UserRight] ");
			strSql.Append(" where UserRightID=@UserRightID ");
			SqlParameter[] parameters = {
					new SqlParameter("@UserRightID", SqlDbType.Int,4)};
			parameters[0].Value = UserRightID;

			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["UserRightID"]!=null && ds.Tables[0].Rows[0]["UserRightID"].ToString()!="")
				{
					this.UserRightID=int.Parse(ds.Tables[0].Rows[0]["UserRightID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["UserID"]!=null && ds.Tables[0].Rows[0]["UserID"].ToString()!="")
				{
					this.UserID=int.Parse(ds.Tables[0].Rows[0]["UserID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ModuleID"]!=null && ds.Tables[0].Rows[0]["ModuleID"].ToString()!="")
				{
					this.ModuleID=int.Parse(ds.Tables[0].Rows[0]["ModuleID"].ToString());
				}
			}
		}

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{

		return DbHelperSQL.GetMaxID("UserRightID", "UserRight"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int UserRightID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from [UserRight]");
			strSql.Append(" where UserRightID=@UserRightID ");

			SqlParameter[] parameters = {
					new SqlParameter("@UserRightID", SqlDbType.Int,4)};
			parameters[0].Value = UserRightID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into [UserRight] (");
			strSql.Append("UserID,ModuleID)");
			strSql.Append(" values (");
			strSql.Append("@UserID,@ModuleID)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.Int,4),
					new SqlParameter("@ModuleID", SqlDbType.Int,4)};
			parameters[0].Value = UserID;
			parameters[1].Value = ModuleID;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update [UserRight] set ");
			strSql.Append("UserID=@UserID,");
			strSql.Append("ModuleID=@ModuleID");
			strSql.Append(" where UserRightID=@UserRightID ");
			SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.Int,4),
					new SqlParameter("@ModuleID", SqlDbType.Int,4),
					new SqlParameter("@UserRightID", SqlDbType.Int,4)};
			parameters[0].Value = UserID;
			parameters[1].Value = ModuleID;
			parameters[2].Value = UserRightID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int UserRightID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from [UserRight] ");
			strSql.Append(" where UserRightID=@UserRightID ");
			SqlParameter[] parameters = {
					new SqlParameter("@UserRightID", SqlDbType.Int,4)};
			parameters[0].Value = UserRightID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public void GetModel(int UserRightID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select UserRightID,UserID,ModuleID ");
			strSql.Append(" FROM [UserRight] ");
			strSql.Append(" where UserRightID=@UserRightID ");
			SqlParameter[] parameters = {
					new SqlParameter("@UserRightID", SqlDbType.Int,4)};
			parameters[0].Value = UserRightID;

			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["UserRightID"]!=null && ds.Tables[0].Rows[0]["UserRightID"].ToString()!="")
				{
					this.UserRightID=int.Parse(ds.Tables[0].Rows[0]["UserRightID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["UserID"]!=null && ds.Tables[0].Rows[0]["UserID"].ToString()!="")
				{
					this.UserID=int.Parse(ds.Tables[0].Rows[0]["UserID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ModuleID"]!=null && ds.Tables[0].Rows[0]["ModuleID"].ToString()!="")
				{
					this.ModuleID=int.Parse(ds.Tables[0].Rows[0]["ModuleID"].ToString());
				}
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM [UserRight] ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		#endregion  Method
	}
}

